local hx__shanghaoqi_skill = fk.CreateSkill{
  name = "#hx__shanghaoqi_skill",
  attached_equip = "hx__shanghaoqi",
}

local U = require "packages/utility/utility"

hx__shanghaoqi_skill:addEffect(fk.RoundStart, {
  global = true,
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    if player.room:getBanner("RoundCount") ~= 1 then return false end
    local to = table.filter(player.room:getAlivePlayers(), function (p)
      return (p.general == "hxqunyou__wuzuge" or p.deputyGeneral == "hxqunyou__wuzuge" or p:hasSkill("hx__zuhao",true, false)
    and not table.find(p:getEquipments(Card.SubtypeTreasure), function(id)
      return Fk:getCardById(id).name == "hx__shanghaoqi"
    end))
    end)
    if #to == 0 then return false end
    local catapult = table.find(U.prepareDeriveCards(player.room, {{ "hx__shanghaoqi"}}, hx__shanghaoqi_skill.name), function (id)
      return player.room:getCardArea(id) == Card.Void
    end)
    if catapult then
      for _, p in ipairs(to) do
        if p:canMoveCardIntoEquip(catapult) and not table.find(p:getEquipments(Card.SubtypeTreasure), function(id)
          return Fk:getCardById(id).name == "hx__shanghaoqi" end) then
          return true
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = table.filter(player.room:getAlivePlayers(), function (p)
      return p.general == "hxqunyou__wuzuge" or p.deputyGeneral == "hxqunyou__wuzuge" or p:hasSkill("hx__zuhao",true, false)
    end)
    for _, p in ipairs(to) do
      local catapult = table.find(U.prepareDeriveCards(room, {{ "hx__shanghaoqi"}}, "hx__shanghaoqi_skill:"..p.id), function (id)
        return player.room:getCardArea(id) == Card.Void
      end)
      if catapult then
        if p:canMoveCardIntoEquip(catapult) then
          room:setCardMark(Fk:getCardById(catapult), MarkEnum.DestructIntoDiscard, 1)
          room:moveCardIntoEquip(p, catapult, hx__shanghaoqi_skill.name, true)
        end
      end
    end
  end,
})

hx__shanghaoqi_skill:addEffect(fk.RoundEnd,{
  global = true,
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    local to = table.filter(player.room:getAlivePlayers(), function (p)
      return (p.general == "hxqunyou__wuzuge" or p.deputyGeneral == "hxqunyou__wuzuge" or p:hasSkill("hx__zuhao",true, false)
    and not table.find(p:getEquipments(Card.SubtypeTreasure), function(id)
      return Fk:getCardById(id).name == "hx__shanghaoqi"
    end))
    end)
    if #to == 0 then return false end
    local catapult = table.find(U.prepareDeriveCards(player.room, {{ "hx__shanghaoqi"}}, hx__shanghaoqi_skill.name), function (id)
      return player.room:getCardArea(id) == Card.Void
    end)
    if catapult then
      for _, p in ipairs(to) do
        if p:canMoveCardIntoEquip(catapult) and not table.find(p:getEquipments(Card.SubtypeTreasure), function(id)
          return Fk:getCardById(id).name == "hx__shanghaoqi" end) then
          return true
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = table.filter(player.room:getAlivePlayers(), function (p)
      return p.general == "hxqunyou__wuzuge" or p.deputyGeneral == "hxqunyou__wuzuge" or p:hasSkill("hx__zuhao",true, false)
    end)
    for _, p in ipairs(to) do
      local catapult = table.find(U.prepareDeriveCards(room, {{ "hx__shanghaoqi"}}, "hx__shanghaoqi_skill:"..p.id), function (id)
        return player.room:getCardArea(id) == Card.Void
      end)
      if catapult then
        if p:canMoveCardIntoEquip(catapult) then
          room:setCardMark(Fk:getCardById(catapult), MarkEnum.DestructIntoDiscard, 1)
          room:moveCardIntoEquip(p, catapult, hx__shanghaoqi_skill.name, true)
        end
      end
    end
  end,
})


hx__shanghaoqi_skill:addEffect(fk.BeforeMaxHpChanged, {
  anim_type = "defensive",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(hx__shanghaoqi_skill.name)
  end,
  on_cost = function(self, event, target, player, data)
    local num = math.abs(data.num)
    if data.num > 0 then
      return player.room:askToSkillInvoke(player, {
        skill_name = hx__shanghaoqi_skill.name,
        prompt = "是否发动【上号器】将你即将增加的"..num.."点体力上限改为回复等量的体力值？"
      })
    elseif data.num < 0 then
      return player.room:askToSkillInvoke(player, {
        skill_name = hx__shanghaoqi_skill.name,
        prompt = "是否发动【上号器】将你即将减少的"..num.."点体力上限改为失去两倍的体力值？"
      })
    end
    return false
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local num = data.num
    data.num = 0
    if num > 0 then
      room:recover({ who = player, num = num, skillName = hx__shanghaoqi_skill.name })
    elseif num < 0 then
      room:loseHp(player, -num * 2, hx__shanghaoqi_skill.name)
    end
  end,
})

return hx__shanghaoqi_skill